package com.hzdesk.media.koreabatchmatvector.logic.view;

import android.Manifest;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.preference.PreferenceManager;
import android.support.annotation.Nullable;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.transition.Fade;
import android.util.Log;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.LinearInterpolator;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.hzdesk.media.koreabatchmatvector.R;
import com.hzdesk.media.koreabatchmatvector.base.MyClientApplication;
import com.hzdesk.media.koreabatchmatvector.bean.UserBean;
import com.hzdesk.media.koreabatchmatvector.config.AppConfig;
import com.hzdesk.media.koreabatchmatvector.logic.contract.SplashContract;
import com.hzdesk.media.koreabatchmatvector.logic.persenter.SplashPersenter;
import com.hzdesk.media.koreabatchmatvector.utils.SharedPreferencesUtils;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2018/6/22.
 * 杭州席媒科技有限公司  Developers  刘传华  11
 * 佛祖镇楼 bug辟易
 */

public class SplashActivity extends BaseActivity implements SplashContract.ISplashView {
    private String TAG="SplashActivity";
    @BindView(R.id.activity_aplash_iv_logo)
    ImageView activityAplashIvLogo;
    @BindView(R.id.activity_aplash_iv_desc)
    ImageView activityAplashIvDesc;
    @BindView(R.id.activity_aplash_iv_start)
    ImageView activityAplashIvStart;
    @BindView(R.id.activity_aplash_tv_start)
    TextView activityAplashTvStart;

    @BindView(R.id.activity_aplash_iv_progress)
    ImageView activityAplashIvProgress;
    @BindView(R.id.activity_aplash_tv_desc)
    TextView activityAplashTvDesc;
    @BindView(R.id.activity_aplash_rl_bg)
    RelativeLayout activityAplashRlBg;

    private static String[] PERMISSIONS_STORAGE = {
            Manifest.permission.READ_EXTERNAL_STORAGE,
            Manifest.permission.WRITE_EXTERNAL_STORAGE };
    private static final int REQUEST_EXTERNAL_STORAGE = 1;

    private SplashPersenter mSplashPersenter = new SplashPersenter(this);
    private Handler mhandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 1:
                    activityAplashTvDesc.setText("读取本地视频文件");
                    mhandler.sendEmptyMessageDelayed(2, 2000);
                    break;
                case 2:
                    activityAplashTvDesc.setText("载入中...");
                    mhandler.sendEmptyMessageDelayed(3, 1500);
                    break;
                case 3:
                    mSplashPersenter.OnClickToMainPager();
                    break;
            }
        }
    };

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().setEnterTransition(new Fade().setDuration(2000));
        getWindow().setExitTransition(new Fade().setDuration(2000));
        setContentView(R.layout.activity_aplash);
        int permission = ActivityCompat.checkSelfPermission(this,
                Manifest.permission.WRITE_EXTERNAL_STORAGE);
        if (permission != PackageManager.PERMISSION_GRANTED) {
            // We don't have permission so prompt the user
            ActivityCompat.requestPermissions(this, PERMISSIONS_STORAGE,
                    REQUEST_EXTERNAL_STORAGE);
        }
        ButterKnife.bind(this);
        initDate();
    }

    private void initDate() {
        mSplashPersenter.initUserList(this);

        mSplashPersenter.NetworkRequest(this);
        // todo 拷贝文件
        mSplashPersenter.CopyFile(this);
    }

    @Override
    public void toMainPager() {
        if (SharedPreferencesUtils.isFirstRun(this)){
            Log.e(TAG,"isFirstRun");
            SharedPreferencesUtils sharedPreferencesUtils=SharedPreferencesUtils.getInstance(this);
            sharedPreferencesUtils.putDate(AppConfig.IS_FIRST_RUN,false);
            Intent  intent =  new Intent(SplashActivity.this, MainActivity.class);
            startActivity(intent);
            finish();
        }else {
            Intent  intent =  new Intent(SplashActivity.this, MainActivity.class);
            startActivity(intent);
            finish();
        }

    }

    // 添加用户列表
    @Override
    public void addUserList(List<UserBean> list) {
    }

    @Override
    public void showDialog() {

    }

    @Override
    protected void onStart() {
        super.onStart();
        final SharedPreferences mSharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
        int selector = mSharedPreferences.getInt("item_skin", 0); //todo 名称
        activityAplashRlBg.setBackgroundResource(updateBg[selector]);

        activityAplashTvDesc.setText("正在检测视频信号");
        Animation circle_anim = AnimationUtils.loadAnimation(this, R.anim.anim_round_rotate);
        LinearInterpolator interpolator = new LinearInterpolator();  //设置匀速旋转，在xml文件中设置会出现卡顿
        circle_anim.setInterpolator(interpolator);
        if (circle_anim != null) {
            activityAplashIvProgress.startAnimation(circle_anim);  //开始动画
        }
        mhandler.sendEmptyMessageDelayed(1, 1500);
    }

}
